bitkeeper revision 1.1392 (4270eaf9Zx1D03KOAVKHY8tW-aANmg)
authormjw@wray-m-3.hpl.hp.com <mjw@wray-m-3.hpl.hp.com>
Thu, 28 Apr 2005 13:54:01 +0000 (13:54 +0000)
committermjw@wray-m-3.hpl.hp.com <mjw@wray-m-3.hpl.hp.com>
Thu, 28 Apr 2005 13:54:01 +0000 (13:54 +0000)
Apply Nguyen's patch:

- extend filesystem abstraction by adding file_exist() method. this
method is used to check for existent of a file given its name. now
ext2fs implements this method.
- pygrub opens and parses /boot/grub/menu.lst or /boot/grub/grub.conf,
in that order.
- add /usr/lib/python to system path (see pygrub). without this
change, pygrub cannot find grub python package.
- remove few blank lines

Signed-off-by: Nguyen Anh Quynh <aquynh@gmail.com>
Signed-off-by: Mike Wray <mike.wray@hp.com>
tools/pygrub/src/fsys/__init__.py
tools/pygrub/src/fsys/ext2/ext2module.c
tools/pygrub/src/pygrub

index 6d76301deb1a3f93958e4bbdf746b6567f501a8b..e669986ee86eee9fef3a53c9d18aa15575073c79 100644 (file)
@@ -49,7 +49,10 @@ class FileSystem(object):
         should look similar to a native file object."""
         raise RuntimeError, "open_file not implemented"
     
-
+    def file_exist(self, file):
+        """Check to see if the give file is existed.
+        Return true if file existed, return false otherwise."""
+        raise RuntimeError, "file_exist not implemented"
 
 mydir = sys.modules['grub.fsys'].__path__[0]
 for f in os.listdir(mydir):
index 30cfd043ae2aa10a39b4f52982d122efab91bc97..ed81c3a1dca4c25e36c61bb4fe43037c7936aeb9 100644 (file)
@@ -174,6 +174,25 @@ ext2_file_open (Ext2Fs *fs, char * name, int flags)
     return (PyObject *) file;
 }
 
+static PyObject *
+ext2_file_exist (Ext2Fs *fs, char * name)
+{
+    int err;
+    ext2_ino_t ino;
+    Ext2File * file;
+
+    file = (Ext2File *) PyObject_NEW(Ext2File, &Ext2FileType);
+    file->file = NULL;
+
+    err = ext2fs_namei_follow(fs->fs, EXT2_ROOT_INO, EXT2_ROOT_INO, name, &ino);
+    if (err) {
+        Py_INCREF(Py_False);
+        return Py_False;
+    }
+    Py_INCREF(Py_True);
+    return Py_True;
+}
+
 /* ext2fs object */
 
 static PyObject *
@@ -231,6 +250,18 @@ ext2_fs_open_file (Ext2Fs *fs, PyObject *args, PyObject *kwargs)
     return ext2_file_open(fs, name, flags);
 }
 
+static PyObject *
+ext2_fs_file_exist (Ext2Fs *fs, PyObject *args, PyObject *kwargs)
+{
+    static char *kwlist[] = { "name", NULL };
+    char * name;
+
+    if (!PyArg_ParseTupleAndKeywords(args, kwargs, "s", kwlist, &name))
+                                     return NULL;
+
+    return ext2_file_exist(fs, name);
+}
+
 static void
 ext2_fs_dealloc (Ext2Fs * fs)
 {
@@ -249,6 +280,9 @@ static struct PyMethodDef Ext2FsMethods[] = {
         { "open_file",
           (PyCFunction) ext2_fs_open_file,
           METH_VARARGS|METH_KEYWORDS, NULL },
+        { "file_exist",
+          (PyCFunction) ext2_fs_file_exist,
+          METH_VARARGS|METH_KEYWORDS, NULL },
        { NULL, NULL, 0, NULL } 
 };
 
@@ -312,21 +346,20 @@ ext2_fs_new(PyObject *o, PyObject *args, PyObject *kwargs)
     return (PyObject *)pfs;
 }
 
-
 static struct PyMethodDef Ext2ModuleMethods[] = {
     { "Ext2Fs", (PyCFunction) ext2_fs_new, METH_VARARGS|METH_KEYWORDS, NULL },
     { NULL, NULL, 0, NULL }
 };
 
-
 void init_pyext2(void) {
-    PyObject *m, *d;
+    PyObject *m;
 
     m = Py_InitModule("_pyext2", Ext2ModuleMethods);
-    d = PyModule_GetDict(m);
-
-    /*    o = PyObject_NEW(PyObject, yExt2FsConstructorType);
-    PyDict_SetItemString(d, "PyExt2Fs", o);
-    Py_DECREF(o);*/
-                      
+    /*
+     * PyObject *d;
+     * d = PyModule_GetDict(m);
+     * o = PyObject_NEW(PyObject, yExt2FsConstructorType);
+     * PyDict_SetItemString(d, "PyExt2Fs", o);
+     * Py_DECREF(o);
+     */
 }
index 4439092625c82a243404e17bc2b80dd53b04eb96..545feea54fd9d38fd1588e2134d2d6e4a972bbc1 100644 (file)
@@ -19,6 +19,8 @@ import logging
 import curses, _curses, curses.wrapper
 import getopt
 
+sys.path = [ '/usr/lib/python' ] + sys.path
+
 import grub.GrubConf
 import grub.fsys
 
@@ -78,7 +80,6 @@ def is_disk_image(file):
     if len(buf) >= 512 and struct.unpack("H", buf[0x1fe: 0x200]) == (0xaaff):
         return True
     return False
-    
 
 def get_config(fn):
     if not os.access(fn, os.R_OK):
@@ -97,7 +98,14 @@ def get_config(fn):
             break
 
     if fs is not None:
-        f = fs.open_file("/boot/grub/grub.conf")
+        if fs.file_exist("/boot/grub/menu.lst"):
+            grubfile = "/boot/grub/menu.lst"
+        elif fs.file_exist("/boot/grub/grub.conf"):
+            grubfile = "/boot/grub/grub.conf"
+        else:
+            raise RuntimeError, "we couldn't find /boot/grub{menu.lst,grub.conf} " + \
+                                "in the image provided. halt!"
+        f = fs.open_file(grubfile)
         buf = f.read()
         f.close()
         fs.close()